﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Fuse8.ContentManagementFramework.BizFacade.Services;
using Fuse8.ContentManagementFramework.BizFacade.Services.Contents;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.SystemFacade;
using Fuse8.DomainFramework.BusinessService;

namespace Fuse8.ContentManagementFramework.Web.ApplicationRuntime.ContentModuleModels
{
    [Serializable]
    public class ContentModuleModel
    {
        public ContentModuleDataSource[] Sources { get; set; }
        public string Template { get; set; }
        public int? TemplateId { get; set; }

        public IEnumerable<SelectListItem> ContentTypes(int? id)
        {

            return
                new BusinessServiceFacade<CmsContentType, int>().FindAll().OrderBy(p => p.ContentTypeName).Select(
                    p =>
                    new SelectListItem()
                        {Text = p.ContentTypeName, Value = p.Id.ToString(), Selected = id.GetValueOrDefault(0) == p.Id});

        }

        public IEnumerable<SelectListItem> ContentFields(int? typeId, string fieldName)
        {
            var list = new List<CmsEntityMetaFieldLink>();

            var type = new BusinessServiceFacade<CmsContentType, int>().FindOne(typeId.GetValueOrDefault());

            if (type != null)
            {
                list.Add(CreatePredefined("Id", "Id"));
                list.Add(CreatePredefined("ContentTitle", "Title"));
                list.Add(CreatePredefined("DateCreated", "Date Created"));
                list.Add(CreatePredefined("DateLastUpdated", "Date Last Updated"));
                if (type.IsBodyEditable)
                {
                    list.Add(CreatePredefined("ContentBody", "Body"));
                }
                if (type.IsSummaryEditable)
                {
                    list.Add(CreatePredefined("ContentSummary", "Summary"));
                }
                var metaFields = new CmsEntityMetaFieldLinkService().FindForEntityType(CmsConstants.ContentParameter + type.Id);
                list.AddRange(metaFields);

            }

            return list.Select(p => new SelectListItem()
                                              {
                                                  Value = p.EntityMetaField.MetaFieldName,
                                                  Text = p.DisplayLabel,
                                                  Selected = p.EntityMetaField.MetaFieldName == fieldName
                                              });
        }

        private static CmsEntityMetaFieldLink CreatePredefined(string name, string label)
        {
            return new CmsEntityMetaFieldLink()
                       {
                           EntityMetaField = new CmsEntityMetaField() {MetaFieldName = name},
                           DisplayLabel = label
                       };
        }
    }    
}